package com.scaffold.card.action;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import wangjg.commons.page.Page;

import com.scaffold.BasicBizAction;
import com.scaffold.card.entity.SimpleCard;
import com.scaffold.card.service.SimpleCardService;

/**
 * 卡片操作接口(json)
 * @author wangjg
 */
@Controller
public class SimpleCardJsonAction extends BasicBizAction{
	@Autowired
	private SimpleCardService simpleCardService;

	/**
	 * 卡片列表
	 * @param keyword 查询关键字
	 */
	@RequestMapping("/simpleCard/list.json")
	public String list(Long categoryId, String keyword){
		Page page = this.getPage(20);
		List<SimpleCard> list = simpleCardService.pageQuery(page, categoryId, keyword);
		return this.getPageResult(page, list);
	}
	
	/**
	 * 获取卡片
	 * @param id 编号
	 * @return
	 */
	@RequestMapping("/simpleCard/get.json")
	public String get(Long id){
		SimpleCard simpleCard = simpleCardService.getSimpleCard(id);
		this.checkOwner(simpleCard);
		return this.getSuccessResult(simpleCard);
	}

	/**
	 * 保存卡片
	 */
	@RequestMapping("/simpleCard/save.json")
	public String save(Long id){
		if(id==null){
			SimpleCard simpleCard = new SimpleCard();
			this.bindParams(simpleCard, "enabled");
			this.setOwner(simpleCard);
			this.simpleCardService.save(simpleCard);
		}else{
			SimpleCard simpleCard = this.simpleCardService.getSimpleCard(id);
			this.checkOwner(simpleCard);
			this.bindParams(simpleCard, "enabled");
			this.simpleCardService.update(simpleCard);
		}
		return this.getSuccessResult();
	}

	/**
	 * 删除卡片
	 * @param id 编号
	 * @return
	 */
	@RequestMapping("/simpleCard/delete.json")
	public String delete(Long id){
		SimpleCard simpleCard = simpleCardService.getSimpleCard(id);
		this.checkOwner(simpleCard);
		this.simpleCardService.delete(simpleCard);
		return this.getSuccessResult();
	}
	
	/**
	 * 启用/禁用卡片
	 * @param id 编号
	 * @param enabled 是否启用
	 * @return
	 */
	@RequestMapping("/simpleCard/enable.json")
	public String enable(Long id, String enabled){
		if(!"Y".equals(enabled)){
			enabled = "N";
		}
		SimpleCard simpleCard = simpleCardService.getSimpleCard(id);
		this.checkOwner(simpleCard);
		simpleCard.setEnabled(enabled);
		this.simpleCardService.update(simpleCard);
		return this.getSuccessResult(enabled);
	}

	/**
	 * 获取卡片列表
	 * @param keyword
	 * @return
	 */
	@RequestMapping("/simpleCard/select.json")
	public String select(Page page, Long categoryId, String keyword){
		List<SimpleCard> list = simpleCardService.pageQuery(page, categoryId, keyword);
		return this.getSuccessResult(list);
	}

}
